﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.IO;

namespace zombieArena
{
    class HighScores
    {
        List<Score> scores;
        public HighScores()
        {
            scores = new List<Score>();
            getScores();
        }

        public void orderScores()
        {
            scores.Sort((s1, s2) => s2.points.CompareTo(s1.points));    
        }

        public void getScores()
        {
            try
            {
                using (StreamReader sr = new StreamReader("Content/highscores.txt"))
                {

                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        string[] parts = line.Split(',');
                        String name = parts[0];
                        int points = Convert.ToInt32(parts[1]);
                        int round = Convert.ToInt32(parts[2]);
                        Score thisScore = new Score(name, points, round);
                        scores.Add(thisScore);
                    }
                    sr.Close();
                }
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine("File not found");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unkonwn Error");
            }
            orderScores();

            foreach (Score s in scores)
            {
                Console.WriteLine("Name: " + s.name + " Points: " + s.points + " Round: " + s.round);
            }


        }
        
        public void inputScore(String name, int points, int round)
        {
            Score newScore = new Score(name, points, round);
            scores.Add(newScore);
            orderScores();
            /*
            FileStream fs = new FileStream("Content/highscores.txt", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);

            for (int i = 0; i < 10; i++)
            {
                if (i < scores.Count)
                    sw.WriteLine(scores[i].name + "," + scores[i].points + "," + scores[i].round + ",");
            }

            
            sw.Close();
            fs.Close();
            */
            
        }
        public void drawHighScores(Texture2D bg, ref SpriteBatch spriteBatch, SpriteFont hsFont)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(bg, new Vector2(0, 0), Color.White);

            spriteBatch.DrawString(hsFont, "Name", new Vector2(200, 250), Color.White);
            spriteBatch.DrawString(hsFont, "Points", new Vector2(300, 250), Color.White);
            spriteBatch.DrawString(hsFont, "Round", new Vector2(410, 250), Color.White); 
            
            for (int i = 0; i < 10; i++)
            {
                if (i < scores.Count)
                {
                    
                    spriteBatch.DrawString(hsFont, (i+1)+".", new Vector2(150, 300 + i*30), Color.White);
                    spriteBatch.DrawString(hsFont, scores[i].name, new Vector2(210, 300 + i * 30), Color.White);
                    spriteBatch.DrawString(hsFont, ""+scores[i].points, new Vector2(300, 300 + i * 30), Color.White);
                    spriteBatch.DrawString(hsFont, ""+scores[i].round, new Vector2(420, 300 + i * 30), Color.White); 
                }

            }

            spriteBatch.DrawString(hsFont, "PRESS A TO CONTINUE", new Vector2(300, 650), Color.White); 

            spriteBatch.End();
        }

    }

    class Score
    {
        public String name;
        public int points;
        public int round;
        public Score(String name, int points, int round)
        {
            this.name = name;
            this.points = points;
            this.round = round;
        }

        public static int CompareScores(Score s1, Score s2)
        {
            return s1.points.CompareTo(s2.points);
        }
    }

}
